package chess.pieces;

import abstractgame.GamePiece;
import abstractgame.GameState;

/**
 * 
 * @author Joakim Nilsson
 * 
 */
public class Bishop extends GamePiece {

	public Bishop(GameState state, int[] pos) {
		super(state, pos);
	}

	@Override
	public boolean[][] allowedMoves() {
		// Declare basic variables
		int height = state.getBoardHeight();
		int width = state.getBoardWidth();
		int[] currentPos = new int[2];
		boolean[][] movePattern = new boolean[height][width];
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				movePattern[i][j] = false;
			}
		}
		// Check movement
		double dir; // The direction to check.
		final double lenghtFactor = Math.sqrt(2); // Factor to make position changes integral.
		for (int i = 0; i < 4; i++) {
			dir = Math.PI / 4 + Math.PI / 2 * i;
			// There might be a "copy()" function to compress the 2 following
			// lines.
			currentPos[0] = pos[0];
			currentPos[1] = pos[1];
			while (true) {
				if ((currentPos[0] > 0) && (currentPos[0] < width)) {
					currentPos[0] += Math.round(Math.cos(dir) * lenghtFactor);
				} else {
					break;
				}
				if ((currentPos[1] > 0) && (currentPos[0] < height)) {
					currentPos[1] += Math.round(Math.sin(dir) * lenghtFactor);
				} else {
					break;
				}
				if (state.checkPos(pos) == null) {
					movePattern[currentPos[0]][currentPos[1]] = true;
				} else if (state.checkPos(pos).getColor() == pieceColor) {
					movePattern[currentPos[0]][currentPos[1]] = false;
					break;
				} else {
					movePattern[currentPos[0]][currentPos[1]] = true;
					break;
				}
			}
		}
		return movePattern;
	}
}
